// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once
#ifdef __linux__ || defined(__linux)
#include <assert.h>

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

typedef struct {
    sigar_pid_t pid;
    time_t mtime;
    sigar_uint64_t vsize;
    sigar_uint64_t rss;
    sigar_uint64_t minor_faults;
    sigar_uint64_t major_faults;
    sigar_uint64_t ppid;
    int tty;
    int priority;
    int nice;
    sigar_uint64_t start_time;
    sigar_uint64_t utime;
    sigar_uint64_t stime;
    char name[SIGAR_PROC_NAME_LEN];
    char state;
    int processor;
} linux_proc_stat_t;

typedef enum {
    IOSTAT_NONE,
    IOSTAT_PARTITIONS, /* 2.4 */
    IOSTAT_DISKSTATS, /* 2.6 */
    IOSTAT_SYS /* 2.6 */
} linux_iostat_e;

struct sigar_t {
    SIGAR_T_BASE;
    int pagesize;
    int ram;
    int proc_signal_offset;
    linux_proc_stat_t last_proc_stat;
    int lcpu;
    linux_iostat_e iostat;
    char *proc_net;
    /* Native POSIX Thread Library 2.6+ kernel */
    int has_nptl;
};

#define HAVE_STRERROR_R
#ifndef __USE_XOPEN2K
/* use gnu version of strerror_r */
#define HAVE_STRERROR_R_GLIBC
#endif
#define HAVE_READDIR_R
#define HAVE_GETPWNAM_R
#define HAVE_GETPWUID_R
#define HAVE_GETGRGID_R
#endif